import { baseApiUrl, path } from "../../utils/config";
import { userInfo, userEdit, upload, wechatPhone } from '../../utils/api';
import Toast from '@vant/weapp/toast/toast';
Page({
  data: {
    baseImageUrl: baseApiUrl,
    head_img: "",
    nickname: "",
    full_name: "",
    phone: "",
    birthday: "",
    sex: 1
  },
  async onLoad(options) {
    let { user } = await userInfo();
    let { head_img, nickname, full_name, phone, birthday, sex } = user;
    this.setData({ head_img, nickname, full_name, phone, birthday, sex });
  },
  async onChooseAvatar(e) {
    const { avatarUrl } = e.detail;
    let { src } = await upload(avatarUrl)
    this.setData({ head_img: src });
  },
  async getPhoneNumber(e) {
    const { code, errMsg } = e.detail;
    if (errMsg == "getPhoneNumber:fail user deny") return false;
    let { phone_info } = await wechatPhone({ code });
    this.setData({ phone: phone_info.phoneNumber });
  },
  bindDateChange(e) {
    const { value } = e.detail;
    this.setData({ birthday: value });
  },
  changeSex(e) {
    let { value } = e.currentTarget.dataset;
    this.setData({ sex: value });
  },
  async submit() {
    let { head_img, nickname, full_name, phone, birthday, sex } = this.data;
    await userEdit({
      head_img, nickname, full_name, phone, birthday, sex
    });
    Toast.success('操作成功');
  },
  onShareAppMessage() {
    let poster = app.globalData.poster;
    return {
      path,
      imageUrl: poster
    }
  }
})